package cn.lanqiao.chart;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * socket 服务端
 */
public class TestSockServer {
    //集合来记录信息
    private static Vector<Socket> socketList = new Vector<>();


    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        ExecutorService servicePool = null;
        try {
            //启动服务
            serverSocket = new ServerSocket(8888);
            System.out.println("服务器已经启动...");
            //等待获得请求，并接受请求
            while (true) {
                socket = serverSocket.accept(); // 顾客
                //分配Tony给顾客 new Tony()
                //Tony tony1 = new TestSockServer().new Tony(socket);
                //tony1.start();
                servicePool = Executors.newFixedThreadPool(2);
                servicePool.execute(new TestSockServer().new Tony(socket));
                //记录服务信息
                socketList.add(socket);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            servicePool.shutdown();
        }
    }

    class Tony extends Thread {

        Socket socket;

        public Tony(Socket socket) {
            this.socket = socket;
            this.setName("游客" + socket.getPort());
        }

        @Override
        public void run() {
            DataOutputStream dos = null;
            DataInputStream dis = null;
            String str = null;
            try {
                //建立双向连接
                dos = new DataOutputStream(socket.getOutputStream());
                dis = new DataInputStream(socket.getInputStream());
                while (true) {
                    //读取信息
                    if ((str = dis.readUTF()) != null) {
                        System.out.println("客户端输入内容：" + str);
                        if (str.startsWith("##")) {
                            String tempName = str.substring(2);
                            sendMessage("[系统消息]" + this.getName() + " 修改名字为 " + tempName);  //写入信息
                            this.setName(tempName);
                        } else {
                            sendMessage(this.getName() + "：" + str);  //写入信息
                        }

                    }
                    if (("88").equals(str)) {
                        break;
                    }
                }
            } catch (EOFException e) {
                //移除socket
                sendMessage("[系统消息]" + this.getName() + "离开了聊天室！");
                socketList.remove(this.socket);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                //关闭连接
                try {
                    dis.close();
                    dos.close();
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        public void sendMessage(String message) {
            DataOutputStream dos = null;
            //循环遍历list，发送msg
            for (Socket socket : socketList) {
                try {
                    dos = new DataOutputStream(socket.getOutputStream());
                    dos.writeUTF(message);  //写入信息
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}


